package com.sunwayworld.basemodule.rm.train.question.validator;

import java.util.List;

import com.sunwayworld.basemodule.rm.train.exam.bean.LimsCoreExamQuestionBean;
import com.sunwayworld.basemodule.rm.train.exam.service.LimsCoreExamQuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.sunwayworld.framework.i18n.I18nHelper;
import com.sunwayworld.framework.mybatis.mapper.MatchPattern;
import com.sunwayworld.framework.mybatis.mapper.SearchFilter;
import com.sunwayworld.framework.restful.data.RestJsonWrapperBean;
import com.sunwayworld.framework.validator.data.DataValidator;

@Component
public class LimsCoreQuestionDeactivatableValidator implements DataValidator {
    @Autowired
    private LimsCoreExamQuestionService coreExamQuestionService;

    public boolean doValid(Object... args) {
        RestJsonWrapperBean warpper = (RestJsonWrapperBean) args[0];
        
        List<Long> questionIdList = warpper.parseId(Long.class);
        
        List<LimsCoreExamQuestionBean> examQuestionList = coreExamQuestionService.selectListByFilter(SearchFilter.instance().match("QUESTIONID", questionIdList).filter(MatchPattern.OR));
        
        if (!examQuestionList.isEmpty()) {
            examQuestionList.forEach(e -> {
                addConstraintViolation(e.getExt$Item("questioncontent") + ":" + I18nHelper.getMessage("CORE.MODULE.TRAIN.QUESTION.TIP.QUESTION_ALREADY_BE_USED"));
            });
            return false;
        }
        
        return true;
    }
}